package cn.lanchengkeji.datafillingsystem.service.impl;


import cn.lanchengkeji.datafillingsystem.common.MenuDemoEntity;
import cn.lanchengkeji.datafillingsystem.common.MetaDemoEntity;
import cn.lanchengkeji.datafillingsystem.common.ResponseBean;
import cn.lanchengkeji.datafillingsystem.common.StatusCode;
import cn.lanchengkeji.datafillingsystem.entity.Menu;
import cn.lanchengkeji.datafillingsystem.mapper.MenuMapper;
import cn.lanchengkeji.datafillingsystem.service.IMenuService;
import cn.lanchengkeji.datafillingsystem.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author Yixu
 * @since 2018-11-29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     * @throws Exception
     * @author yuhao
     * @version 2018-12-04 15:59
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseBean insertMenu(Menu menu) throws Exception {

        if (Utils.isEmpty(menu)) {
            return new ResponseBean(StatusCode.IS_NULL, menu);
        }

        List<Menu> searchMenus;

        // 判断菜单名称是否重复
        if (Utils.notEmpty(menu.getTitle())) {
            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
            queryConditions.eq(Menu.TITLE, menu.getTitle());
            searchMenus = this.list(queryConditions);
            if (searchMenus.size() > 0) {
                return new ResponseBean(StatusCode.MENU_NAME_REPEAT, null);
            }
        }

        // 判断菜单path_url是否重复
//        if (Utils.notEmpty(menu.getPath())) {
//            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
//            queryConditions.eq(Menu.PATH, menu.getPath());
//            searchMenus = this.list(queryConditions);
//            if (searchMenus.size() > 0) {
//                return new ResponseBean(StatusCode.MENU_URL_REPEAT, null);
//            }
//        }

        // 判断菜单path_name是否重复
//        if (Utils.notEmpty(menu.getName())) {
//            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
//            queryConditions.eq(Menu.NAME, menu.getName());
//            searchMenus = this.list(queryConditions);
//            if (searchMenus.size() > 0) {
//                return new ResponseBean(StatusCode.MENU_PATH_NAME_REPEAT, null);
//            }
//        }

        // 判断菜单component是否重复
        if (Utils.notEmpty(menu.getComponent())) {
            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
            queryConditions.eq(Menu.COMPONENT, menu.getComponent());
            searchMenus = this.list(queryConditions);
            if (searchMenus.size() > 0) {
                return new ResponseBean(StatusCode.MENU_COMPONENT_REPEAT, null);
            }
        }

        if (this.saveOrUpdate(menu)) {
            return new ResponseBean(StatusCode.OK, menu);
        } else {
            return new ResponseBean(StatusCode.OK_FAIL, null);
        }

    }

    /**
     * 修改菜单
     *
     * @param menu
     * @return
     * @throws Exception
     * @author yuhao
     * @version 2018-12-04 17:18
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseBean updateMenu(Menu menu) throws Exception {
        if (Utils.isEmpty(menu) || Utils.isEmpty(menu.getId())) {
            return new ResponseBean(StatusCode.NOT_FOUND, null);
        }

        Menu oMenu = this.getById(menu.getId());
        if (Utils.isEmpty(oMenu)) {
            return new ResponseBean(StatusCode.MENU_NOT_FOUND, null);
        }

        List<Menu> searchMenus;


        // 判断除自己外是否菜单名称重复
        if (Utils.notEmpty(menu.getTitle())) {
            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
            queryConditions.eq(Menu.TITLE, menu.getTitle()).ne(Menu.ID, menu.getId());
            searchMenus = this.list(queryConditions);
            if (searchMenus.size() > 0) {
                return new ResponseBean(StatusCode.MENU_NAME_REPEAT, null);
            }
        }

        // 判断除自己外是否菜单url重复
//        if (Utils.notEmpty(menu.getPath())) {
//            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
//            queryConditions.eq(Menu.PATH, menu.getPath()).ne(Menu.ID, menu.getId());
//            searchMenus = this.list(queryConditions);
//            if (searchMenus.size() > 0) {
//                return new ResponseBean(StatusCode.MENU_URL_REPEAT, null);
//            }
//        }

        // 判断除自己外菜单path_name是否重复
//        if (Utils.notEmpty(menu.getName())) {
//            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
//            queryConditions.eq(Menu.NAME, menu.getName()).ne(Menu.ID, menu.getId());
//            searchMenus = this.list(queryConditions);
//            if (searchMenus.size() > 0) {
//                return new ResponseBean(StatusCode.MENU_PATH_NAME_REPEAT, null);
//            }
//        }

        // 判断菜单component是否重复
        if (Utils.notEmpty(menu.getComponent())) {
            QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
            queryConditions.eq(Menu.COMPONENT, menu.getComponent()).ne(Menu.ID, menu.getId());
            searchMenus = this.list(queryConditions);
            if (searchMenus.size() > 0) {
                return new ResponseBean(StatusCode.MENU_COMPONENT_REPEAT, null);
            }
        }

        // 非空修改
        if (this.saveOrUpdate(menu)) {
            return new ResponseBean(StatusCode.OK, menu);
        } else {
            return new ResponseBean(StatusCode.OK_FAIL, null);
        }

    }

    /**
     * 删除菜单
     *
     * @param menu 根据id删除菜单
     * @return
     * @throws Exception
     * @author yuhao
     * @version 2018-12-05 10:15
     */
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseBean deleteMenu(Menu menu) throws Exception {

        // 参数不存在
        if(Utils.isEmpty(menu)||Utils.isEmpty(menu.getId())){
            return new ResponseBean(StatusCode.NOT_FOUND, null);
        }

        // 判断当前菜单是否已不存在
        Menu oMenu = this.getById(menu.getId());
        if(Utils.isEmpty(oMenu)){
            return new ResponseBean(StatusCode.MENU_NOT_FOUND,null);
        }

        // 判断对当前菜单是否父级菜单，且父级菜单下是否有子级菜单
        QueryWrapper<Menu> queryConditions = new QueryWrapper<>();
        queryConditions.eq(Menu.PID, menu.getId());
        List<Menu> subMenu = this.list(queryConditions);
        if(oMenu.getPid()==0&&subMenu.size()>0){
            return new ResponseBean(StatusCode.MENU_EXIST_SUB,null);
        }

        // 删除
        if(this.removeById(menu.getId())){
            return new ResponseBean(StatusCode.OK,menu);
        }else{
            return new ResponseBean(StatusCode.OK_FAIL,menu);
        }
    }

    /**
     * 查看单个菜单
     *
     * @param menu
     * @return
     * @author yuhao
     * @version 2018-12-05 10:53
     */
    @Override
    public Menu selectOneMenu(Menu menu) {
        return this.getById(menu.getId());
    }

    /**
     * 查看菜单集合
     * @return
     * @author yuhao
     * @version 2018-12-05 10:53
     */
    @Override
    public List<MenuDemoEntity> selectAllMenu() {

        List<MenuDemoEntity> retList = new ArrayList<>();

        // 先查一级菜单
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Menu.PID,0).orderByAsc(Menu.SORTED);
        List<Menu> menus = this.list(queryWrapper);

        for(Menu menu : menus){
            MenuDemoEntity firstMenu = new MenuDemoEntity();
            MetaDemoEntity firstMeta = new MetaDemoEntity();

            firstMenu.setId(menu.getId());
//            firstMenu.setLabel(menu.getMenuName());
            firstMenu.setPath(menu.getPath());
            firstMenu.setName(menu.getName());
            firstMenu.setRedirect(menu.getRedirect());
            firstMeta.setTitle(menu.getTitle());
            firstMeta.setIcon(menu.getIcon());
            firstMenu.setMeta(firstMeta);
            firstMenu.setSorted(menu.getSorted());

            // 根据一级菜单id查询子级菜单
            QueryWrapper<Menu> subQueryWrapper = new QueryWrapper<>();
            subQueryWrapper.eq(Menu.PID, menu.getId()).orderByAsc(Menu.SORTED);
            List<Menu> subMenus = this.list(subQueryWrapper);
            List<MenuDemoEntity> subEntities = new ArrayList<>();
            for(Menu subMenuMap : subMenus){
                MenuDemoEntity subMenu = new MenuDemoEntity();
                MetaDemoEntity subMeta = new MetaDemoEntity();

                subMenu.setId(subMenuMap.getId());
//                subMenu.setLabel(subMenuMap.getMenuName());
                subMenu.setPath(subMenuMap.getPath());
                subMenu.setName(subMenuMap.getName());
                subMenu.setComponent(subMenuMap.getComponent());
                subMeta.setTitle(subMenuMap.getTitle());
                subMeta.setIcon(subMenuMap.getIcon());
                subMenu.setMeta(subMeta);
                subMenu.setSorted(subMenuMap.getSorted());
                subEntities.add(subMenu);
            }
            firstMenu.setChildren(subEntities);
            retList.add(firstMenu);
        }
        return retList;
    }

    /**
     * 根据角色id查询菜单
     *
     * @param roleId
     * @return
     * @author yuhao 2018-12-07 15:56
     */
    @Override
    public List<MenuDemoEntity> selectMenusByRoleId(Integer roleId) {

        List<MenuDemoEntity> retList = new ArrayList<>();

        // 先查一级菜单
        List<Menu> menus = baseMapper.selectMenusByRoleIdAndPid(0, roleId);

        for (Menu menuMap : menus) {
            MenuDemoEntity firstMenu = new MenuDemoEntity();
            MetaDemoEntity firstMeta = new MetaDemoEntity();

            firstMenu.setId(menuMap.getId());
//            firstMenu.setLabel((String) menuMap.get("menuName"));
            firstMenu.setPath(menuMap.getPath());
            firstMenu.setName(menuMap.getName());
            firstMenu.setComponent(menuMap.getComponent());
            firstMeta.setTitle(menuMap.getTitle());
            firstMeta.setIcon(menuMap.getIcon());
            firstMenu.setRedirect(menuMap.getRedirect());
            firstMenu.setMeta(firstMeta);

            // 根据一级菜单id查询子级菜单
            List<Menu> subMenus = baseMapper.selectMenusByRoleIdAndPid((Integer) menuMap.getId(), roleId);
            List<MenuDemoEntity> subEntities = new ArrayList<>();
            for (Menu subMenuMap : subMenus) {
                MenuDemoEntity subMenu = new MenuDemoEntity();
                MetaDemoEntity subMeta = new MetaDemoEntity();

                subMenu.setId(subMenuMap.getId());
                subMenu.setPath(subMenuMap.getPath());
                subMenu.setName(subMenuMap.getName());
                subMeta.setTitle(subMenuMap.getTitle());
                subMeta.setIcon(subMenuMap.getIcon());
                subMenu.setMeta(subMeta);
                subEntities.add(subMenu);
            }
            firstMenu.setChildren(subEntities);
            retList.add(firstMenu);
        }
        return retList;
    }

    @Override
    public List<MenuDemoEntity> selectMenusByRoleIds(List<Integer> roleIds) {
        List<MenuDemoEntity> retList = new ArrayList<>();

        // 先查一级菜单
        List<Menu> menus = baseMapper.selectMenusByRoleIdsAndPid(0, roleIds);

        for (Menu menuMap : menus) {
            MenuDemoEntity firstMenu = new MenuDemoEntity();
            MetaDemoEntity firstMeta = new MetaDemoEntity();

            firstMenu.setId(menuMap.getId());
//            firstMenu.setLabel((String) menuMap.get("menuName"));
            firstMenu.setPath(menuMap.getPath());
            firstMenu.setName(menuMap.getName());
            firstMenu.setComponent(menuMap.getComponent());
            firstMeta.setTitle(menuMap.getTitle());
            firstMeta.setIcon(menuMap.getIcon());
            firstMenu.setRedirect(menuMap.getRedirect());
            firstMenu.setMeta(firstMeta);

            // 根据一级菜单id查询子级菜单
            List<Menu> subMenus = baseMapper.selectMenusByRoleIdsAndPid((Integer) menuMap.getId(), roleIds);
            List<MenuDemoEntity> subEntities = new ArrayList<>();
            for (Menu subMenuMap : subMenus) {
                MenuDemoEntity subMenu = new MenuDemoEntity();
                MetaDemoEntity subMeta = new MetaDemoEntity();

                subMenu.setId(subMenuMap.getId());
                subMenu.setPath(subMenuMap.getPath());
                subMenu.setName(subMenuMap.getName());
                subMeta.setTitle(subMenuMap.getTitle());
                subMeta.setIcon(subMenuMap.getIcon());
                subMenu.setMeta(subMeta);
                subEntities.add(subMenu);
            }
            firstMenu.setChildren(subEntities);
            retList.add(firstMenu);
        }
        return retList;
    }

}
